﻿using System;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Sharp.Platform.Mvc.Bootstrap
{
    public class TextareaBuilder : TagElementBuilder<TextareaBuilder>
    {
        #region Field

        private readonly TypeConfiguration _typeConfiguration;
        private readonly SizeConfiguration<TextareaBuilder> _sizeConfiguration;
        private readonly GroupConfiguration _groupConfiguration;

        #endregion Field

        #region Constructor

        public TextareaBuilder()
        {
            _typeConfiguration = new TypeConfiguration(this);
            _sizeConfiguration = new InputSizeConfiguration(this);
            _groupConfiguration = new GroupConfiguration();
        }

        #endregion Constructor

        #region Public Method

        public TextareaBuilder Placeholder(string placeholder)
        {
            return Attribute("placeholder", placeholder, true);
        }

        public TypeConfiguration Type()
        {
            return _typeConfiguration;
        }

        public TextareaBuilder Disabled(bool disabled = true)
        {
            if (disabled)
                return Attribute("disabled", "disabled", true);
            TagBuilder.Attributes.Remove("disabled");
            return this;
        }

        public SizeConfiguration<TextareaBuilder> Size()
        {
            return _sizeConfiguration;
        }

        public TextareaBuilder Group(Action<GroupConfiguration> configuration)
        {
            configuration(_groupConfiguration);
            return this;
        }

        public TextareaBuilder Value(string value)
        {
            TagBuilder.InnerHtml = value;
            return this;
        }

        #endregion Public Method

        #region Overrides of TagElementBuilder<TextareaBuilder>

        public override string TagName
        {
            get { return "textarea"; }
        }

        /// <summary>
        /// 返回 HTML 编码的字符串。
        /// </summary>
        /// <returns>
        /// HTML 编码的字符串。
        /// </returns>
        public override string ToHtmlString()
        {
            Attribute("type", _typeConfiguration.ToString(), true).AddClass("form-control").AddClass(_sizeConfiguration.ToString());

            var builder = new StringBuilder();
            builder.AppendLine(TagBuilder.ToString(TagRenderMode.StartTag));
            builder.Append(TagBuilder.InnerHtml);
            builder.Append(TagBuilder.ToString(TagRenderMode.EndTag));

            return _groupConfiguration.Build(builder.ToString());
        }

        #endregion Overrides of TagElementBuilder<TextareaBuilder>

        #region Overrides of Object

        /// <summary>
        /// 返回表示当前 <see cref="T:System.Object"/> 的 <see cref="T:System.String"/>。
        /// </summary>
        /// <returns>
        /// <see cref="T:System.String"/>，表示当前的 <see cref="T:System.Object"/>。
        /// </returns>
        public override string ToString()
        {
            return ToHtmlString();
        }

        #endregion Overrides of Object

        #region Help Class

        public sealed class TypeConfiguration
        {
            private readonly TextareaBuilder _textareaBuilder;
            private string _type;

            public TypeConfiguration(TextareaBuilder textareaBuilder)
            {
                if (textareaBuilder == null)
                    throw new ArgumentNullException(nameof(textareaBuilder));

                _textareaBuilder = textareaBuilder;
                //Text();
            }



            public TextareaBuilder Custom(string type)
            {
                _type = type;

                return _textareaBuilder;
            }

            #region Overrides of Object

            /// <summary>
            /// 返回表示当前 <see cref="T:System.Object"/> 的 <see cref="T:System.String"/>。
            /// </summary>
            /// <returns>
            /// <see cref="T:System.String"/>，表示当前的 <see cref="T:System.Object"/>。
            /// </returns>
            public override string ToString()
            {
                return _type;
            }

            #endregion Overrides of Object
        }

        public sealed class InputSizeConfiguration : SizeConfiguration<TextareaBuilder>
        {
            public InputSizeConfiguration(TextareaBuilder tagElementBuilder)
                : base("textarea", tagElementBuilder)
            {
            }
        }

        public sealed class GroupConfiguration
        {
            private readonly GroupSizeConfiguration _groupSizeConfiguration;
            private string _html;
            private bool _isLeft = true;

            public GroupConfiguration()
            {
                _groupSizeConfiguration = new GroupSizeConfiguration(this);
            }

            public GroupConfiguration Html(string html)
            {
                _html = html;
                return this;
            }

            public GroupConfiguration Left()
            {
                _isLeft = true;
                return this;
            }

            public GroupConfiguration Right()
            {
                _isLeft = false;
                return this;
            }

            public GroupSizeConfiguration Size()
            {
                return _groupSizeConfiguration;
            }

            internal string Build(string html)
            {
                if (string.IsNullOrWhiteSpace(_html))
                    return html;

                var builder = new StringBuilder();
                var size = _groupSizeConfiguration.ToString();
                builder.AppendLine(string.Format("<div class=\"input-group{0}\">", string.IsNullOrWhiteSpace(size) ? string.Empty : " " + size));
                if (_isLeft)
                {
                    builder.AppendLine(string.Format("<span class=\"input-group-addon\">{0}</span>", _html));
                    builder.AppendLine(html);
                }
                else
                {
                    builder.AppendLine(html);
                    builder.AppendLine(string.Format("<span class=\"input-group-addon\">{0}</span>", _html));
                }
                builder.AppendLine("</div>");

                return new HtmlString(builder.ToString()).ToHtmlString();
            }

            public sealed class GroupSizeConfiguration : SizeConfiguration<GroupConfiguration>
            {
                public GroupSizeConfiguration(GroupConfiguration tagElementBuilder)
                    : base("input-group", tagElementBuilder)
                {
                }
            }
        }

        #endregion Help Class
    }
}